//
//  LBSuperMarketDetailsVC.m
//  loveBeen
//
//  Created by 韩天助 on 11/20/16.
//  Copyright © 2016 loveBeen. All rights reserved.
//

#import "LBSuperMarketDetailsVC.h"
#import "LBSuperMarketDetailsBaseView.h"
#import "LBSuperMarketDetailsBottomView.h"
#import "LBBuyCarController.h"
#import "LBPlusMinusTool.h"

@interface LBSuperMarketDetailsVC ()<LBSuperMarketDetailsBottomViewDelegate>

@property(weak,nonatomic) LBSuperMarketDetailsBaseView *baseView;
@property(weak,nonatomic) LBSuperMarketDetailsBottomView *bottomView;

@end

@implementation LBSuperMarketDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //    [self setUpUI];
    
    //    NSLog(@"%@",self.productModel);
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    
    [self.view addGestureRecognizer:swipe];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - set方法传值
- (void)setProductModel:(LBSuperMarketProducts *)productModel{
    _productModel = productModel;
    //    NSLog(@"%@",productModel);
    //    self.baseView.productModel = _productModel;
    self.navigationItem.title = productModel.name;
    
    [self setUpUI];
}

#pragma mark - 代理方法
- (void)superMarketDetailsBottomView:(LBSuperMarketDetailsBottomView *)bottomView{
    LBBuyCarController *VC = [[LBBuyCarController alloc]init];
    
    self.navigationController.toolbarHidden = YES;
    [self.tabBarController setSelectedIndex:2];
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)setUpUI{
    
    LBSuperMarketDetailsBaseView *baseView = [[LBSuperMarketDetailsBaseView alloc]initWithProductModel:_productModel];
    self.baseView = baseView;
    //    baseView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:baseView];
    
    baseView.productModel = _productModel;
    //    NSLog(@"%@",baseView.productModel);
    //    NSLog(@"%@",[NSThread currentThread]);
    
    LBSuperMarketDetailsBottomView *bottomView = [[LBSuperMarketDetailsBottomView alloc]init];
    bottomView.backgroundColor = [UIColor cz_colorWithHex:0xf4f4f4];
    self.bottomView = bottomView;
    
    //    LBSuperMarketDetailsBottomView *bottomView =
    [self.view addSubview:bottomView];
    
    bottomView.model = _productModel;
    
    //设置代理
    bottomView.delegate = self;
    
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.top.equalTo(baseView.mas_bottom);
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(47);
    }];
    
}


//即将出现时重新赋值
- (void)viewWillAppear:(BOOL)animated{
    //    self.baseView.productModel = _productModel;
    
    LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];
    
    
    for (NSInteger i = 0; i < manager.selectedProductsModel.count; i++) {
        
        LBSuperMarketProducts *model = (LBSuperMarketProducts *)manager.selectedProductsModel[i];
        if([model.name isEqualToString:self.bottomView.model.name]){
            
//            self.productModel = model;
            _bottomView.model = model;
        }
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
